package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.common.PushtoSingle;
import com.api.shopapi.config.WebSocket;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.SendMessageDTO;
import com.api.shopapi.entity.dto.StaffDTO;
import com.api.shopapi.mapper.StaffMapper;
import com.api.shopapi.service.*;
import com.api.shopapi.util.DateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gexin.rp.sdk.base.IPushResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.java_websocket.server.WebSocketServer;
import com.api.shopapi.entity.AlarmRecord;
import com.api.shopapi.entity.Staff;
import com.api.shopapi.entity.User;
import com.api.shopapi.service.AlarmRecordService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 接收设备平台推送过来的报警信息
 * */

@RequestMapping("Device")
@RestController
public class DeviceController extends BaseController {


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;




    //接收设备服务器发来的手表报警信息
    @RequestMapping(value = "XiaoDouAlarm", method = RequestMethod.POST)
    public MyResult XiaoDouAlarm(@RequestBody JSONObject jsonObject) {

        try {

            String userCode = jsonObject.getString("userCode");

            User user = userService.getByUserCode(userCode);
            user.setPassword(null);

            jsonObject.put("user",user);

            ObjectMapper mapper=new ObjectMapper();
            String pushAlarm = pushAlarmPath;
            File fileAlarm=new File(pushAlarm);
            if(!fileAlarm.exists()){
                fileAlarm.mkdir();
            }

            //为各个后台账号写报警文件
            List<Staff> staffList = staffService.getByGovernmentCodeAndRoleCode(user.getGovernmentCode(), "df7f8940-0512-4e86-a8d4-ab8a8a330fbc");
            for (Staff staff : staffList) {
                String fdPath=pushAlarm+"\\" + staff.getStaffCode();
                File fileStaff=new File(fdPath);
                if(!fileStaff.exists()){
                    fileStaff.mkdir();
                }
                mapper.writeValue(new File(fdPath + "\\xiaodou" + new Date().getTime() + ".txt"),jsonObject);
            }


            //为特殊总账号写报警文件
            String fdPath2=pushAlarm+"\\" + "11111111-1111-1111-1111-111111111111";
            File filev=new File(fdPath2);
            if(!filev.exists()){
                filev.mkdir();
            }
            mapper.writeValue(new File(fdPath2 + "\\xiaodou" + new Date().getTime() + ".txt"),jsonObject);


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    //接收设备服务器发来的手表报警信息
    @RequestMapping(value = "YijuMeshAlarm", method = RequestMethod.POST)
    public MyResult YijuMeshAlarm(@RequestBody JSONObject jsonObject) {
        try {

            String userCode = jsonObject.getString("userCode");

            User user = userService.getByUserCode(userCode);
            user.setPassword(null);

            jsonObject.put("user",user);

            ObjectMapper mapper=new ObjectMapper();
            String pushAlarm = pushAlarmPath;
            File fileAlarm=new File(pushAlarm);
            if(!fileAlarm.exists()){
                fileAlarm.mkdir();
            }

            //为各个后台账号写报警文件
            List<Staff> staffList = staffService.getByGovernmentCodeAndRoleCode(user.getGovernmentCode(), "df7f8940-0512-4e86-a8d4-ab8a8a330fbc");
            for (Staff staff : staffList) {
                String fdPath=pushAlarm+"\\" + staff.getStaffCode();
                File fileStaff=new File(fdPath);
                if(!fileStaff.exists()){
                    fileStaff.mkdir();
                }
                mapper.writeValue(new File(fdPath + "\\yijumesh" + new Date().getTime() + ".txt"),jsonObject);
            }


            //为特殊总账号写报警文件
            String fdPath2=pushAlarm+"\\" + "11111111-1111-1111-1111-111111111111";
            File filev=new File(fdPath2);
            if(!filev.exists()){
                filev.mkdir();
            }
            mapper.writeValue(new File(fdPath2 + "\\yijumesh" + new Date().getTime() + ".txt"),jsonObject);


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    //接收设备服务器发来的睡倍健报警信息
    @RequestMapping(value = "YijuSBJAlarm", method = RequestMethod.POST)
    public MyResult YijuSBJAlarm(@RequestBody JSONObject jsonObject) {
        try {

            String userCode = jsonObject.getString("userCode");

            User user = userService.getByUserCode(userCode);
            user.setPassword(null);

            jsonObject.put("user",user);

            ObjectMapper mapper=new ObjectMapper();
            String pushAlarm = pushAlarmPath;
            File fileAlarm=new File(pushAlarm);
            if(!fileAlarm.exists()){
                fileAlarm.mkdir();
            }

            //为各个后台账号写报警文件
            List<Staff> staffList = staffService.getByGovernmentCodeAndRoleCode(user.getGovernmentCode(), "df7f8940-0512-4e86-a8d4-ab8a8a330fbc");
            for (Staff staff : staffList) {
                String fdPath=pushAlarm+"\\" + staff.getStaffCode();
                File fileStaff=new File(fdPath);
                if(!fileStaff.exists()){
                    fileStaff.mkdir();
                }
                mapper.writeValue(new File(fdPath + "\\yijusbj" + new Date().getTime() + ".txt"),jsonObject);
            }


            //为特殊总账号写报警文件
            String fdPath2=pushAlarm+"\\" + "11111111-1111-1111-1111-111111111111";
            File filev=new File(fdPath2);
            if(!filev.exists()){
                filev.mkdir();
            }
            mapper.writeValue(new File(fdPath2 + "\\yijusbj" + new Date().getTime() + ".txt"),jsonObject);


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    //接收设备服务器发来的App报警信息
    @RequestMapping(value = "AppAlarm", method = RequestMethod.POST)
    public MyResult AppAlarm(@RequestBody JSONObject jsonObject) {
        try {

            String userCode = jsonObject.getString("userCode");

            User user = userService.getByUserCode(userCode);
            user.setPassword(null);

            jsonObject.put("user",user);

            ObjectMapper mapper=new ObjectMapper();
            String pushAlarm = pushAlarmPath;
            File fileAlarm=new File(pushAlarm);
            if(!fileAlarm.exists()){
                fileAlarm.mkdir();
            }

            //为各个后台账号写报警文件
            List<Staff> staffList = staffService.getByGovernmentCodeAndRoleCode(user.getGovernmentCode(), "df7f8940-0512-4e86-a8d4-ab8a8a330fbc");
            for (Staff staff : staffList) {
                String fdPath=pushAlarm+"\\" + staff.getStaffCode();
                File fileStaff=new File(fdPath);
                if(!fileStaff.exists()){
                    fileStaff.mkdir();
                }
                mapper.writeValue(new File(fdPath + "\\app" + new Date().getTime() + ".txt"),jsonObject);
            }


            //为特殊总账号写报警文件
            String fdPath2=pushAlarm+"\\" + "11111111-1111-1111-1111-111111111111";
            File filev=new File(fdPath2);
            if(!filev.exists()){
                filev.mkdir();
            }
            mapper.writeValue(new File(fdPath2 + "\\app" + new Date().getTime() + ".txt"),jsonObject);


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }



}
